﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;

namespace CERT.AcceptanceTests.Features
{
    [Binding]
    public class EditTeamMemberSteps : TestFixtureBase
    {
        [Given(@"When I edit a team member")]
        public void GivenWhenIEditATeamMember(Table table)
        {
            var info = ScenarioContext.Current.ScenarioInfo;
            // Assert.AreEqual("Logging in with valid credentials", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            for (int x = 0; x < 200000; x++)
            { }

           var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

          
               foreach (var row in table.Rows)
               {
                   CurrentDriver.FindElementById(row["field"]).Clear();
                   var textField = CurrentDriver.FindElementById(row["field"]);

                   if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                   textField.SendKeys(row["value"]);
               }
           
        }
        
        [When(@"I press save team member edit")]
        public void WhenIPressSaveTeamMemberEdit()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var savebutton = CurrentDriver.FindElementById("saveMember");
            savebutton.Click();
            WaitUpTo(500, "save failed");

        }
        
        [Then(@"the result should be a revised email")]
        public void ThenTheResultShouldBeARevisedEmail(Table table3)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            WaitUpTo(50, "save failed");
            editbutton.Click();
            WaitUpTo(50, "save failed");
            foreach (var row in table3.Rows)
            {
                
                 var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
               Assert.IsTrue(textField.Text.Contains(row["value"]));

            }
        }


        [Given(@"When I edit a team member phone")]
        public void GivenWhenIEditATeamMemberPhone(Table table5)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table5.Rows)
            {
                CurrentDriver.FindElementById(row["field"]).Clear();
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);

            }
        }

        [Then(@"the result should be a revised phone")]
        public void ThenTheResultShouldBeARevisedPhone(Table table6)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table6.Rows)
            {

                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                Assert.IsTrue(textField.Text.Contains(row["value"]));

            }
        }

        [Given(@"When I edit a team member name")]
        public void GivenWhenIEditATeamMemberName(Table table7)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table7.Rows)
            {
                CurrentDriver.FindElementById(row["field"]).Clear();
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);

            }
        }

        [Then(@"the result should be a revised name")]
        public void ThenTheResultShouldBeARevisedName(Table table8)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table8.Rows)
            {

                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                Assert.IsTrue(textField.Text.Contains(row["value"]));

            }
        }

        [Given(@"When I edit a team member name again")]
        public void GivenWhenIEditATeamMemberNameAgain(Table table11)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table11.Rows)
            {
                CurrentDriver.FindElementById(row["field"]).Clear();
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);

            }
        }


        [When(@"I press cancel team member edit")]
        public void WhenIPressCancelTeamMemberEdit()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var savebutton = CurrentDriver.FindElementById("undoMember");
            savebutton.Click();
            for (int x = 0; x < 200000; x++)
            { }
        }

        [Then(@"the result should be a original name")]
        public void ThenTheResultShouldBeAOriginalName(Table table12)
        {
            var editbutton = CurrentDriver.FindElementById("editMember");
            editbutton.Click();

            foreach (var row in table12.Rows)
            {

                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                Assert.IsTrue(textField.Text.Contains(row["value"]));

            }
        }

    }
}
